// 
//  array.js
//  firefox
//  
//  Created by Zak on 2008-07-03.
//  Copyright 2008 Ant.com. All rights reserved.
// 

top.AntArray = function () {}
AntArray.prototype = new Array;
Array.prototype.constructor = Array;

/**
 * Add array items to the current AntArray
 * @param Array
 */
AntArray.prototype.fromArray = function (arr)
{
	if (!arr || typeof arr != "object")
		return ;
	for (var i = 0; i < arr.length; i++)
		this.push(arr[i]);

	return this;
}

/**
 * Return an array 
 */
AntArray.prototype.toArray = function ()
{
    var newArr = new Array();

    for (var i = 0; i < this.length; i++)
            newArr.push(this[i]);
    return newArr;
}

/**
 * Return true if the given value is in the current array
 * @param value         The value to search
 * @param compare       The function to use to test the presence of a similar data
 * @return (boolean)    True if the value is in the current array, false if not.
 */
AntArray.prototype.inArray = function (value, compare)
{
    for (var i = 0; i < this.length; i++)
    {
        if (compare != undefined)
        {
            if (compare(this[i] , value))
                return true;
        }
        else
        {
            if (this[i] == value)
                return true;
        }
    }

    return false;
}

/**
 * Return an array containing only uniques entry from the given one
 * @param compare       (Optional) The function to compare values with
 * @return newArr       The uniqified array
 */
AntArray.prototype.uniq = function (compare)
{
    var newArr = new AntArray();

    for (var i = 0; i < this.length; i++)
    {
        if (!newArr.inArray(this[i], compare))
            newArr.push(this[i]);
    }

    return newArr;
}

/**
 * Return a boolean value wether a value has been removed or not
 * @param value       value to be removed
 */
AntArray.prototype.removeValue = function(value, compare)
{
    for (var i = 0; i < this.length; i++)
    {
        var cond = (compare ? compare(this[i], value) : (this[i] == value));

        if (cond)
        {
            this.splice(i, 1);

            return true;
        }
    }

    return false;
}

/**
 * Return a boolean true if an element has been removed
 * @param index     index of the array to be removed
 */
AntArray.prototype.removeIndex = function(index)
{
    if (this.length < index)
    {
        this.splice(index, 1);

        return true;
    }

    return false;
}

/**
 * each iterator for the array
 */
AntArray.prototype.each = function (func)
{
	for (var i = 0; i < this.length; i++)
		func(this[i], i);
}

/**
 * toString()
 */
AntArray.prototype.toString = function ()
{
	var s = "AntArray[";

	for (var i = 0; i < this.length; i++)
		s += this[i] +",";

	return s.substr(0, s.length - 1) + "]";
}

